package transportManagement;

import java.io.IOException;
import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import common.Date;
import common.ObjectFileStoring;
import common.Storing;

public class TransportBoard implements Serializable {
	private static final long serialVersionUID = 2089809277507042769L;
	private static final TransportBoard reference = new TransportBoard();
	
	private Storing<Transport> fileStoring;
	
	private static final String PATHNAME = ".";
	private static final String FILENAME = "TransportBoard";
	
	private TransportBoard() {
		fileStoring = new ObjectFileStoring<Transport>(PATHNAME, FILENAME);
	}
	
	public static TransportBoard getInstance() {
		return reference;
	}

	private int getTransportIndex(List<Transport> transportList, Transport transport) {
		int i = 0;
		while(i < transportList.size() && !transportList.get(i).equals(transport)) {
			i++;
		}
		
		if(i >= transportList.size()) {
			// corsa non presente nella lista
			i = -1;
		}
		
		return i;
	}
	
	public synchronized void add(Transport transport) throws InvalidParameterException {
		if(transport == null) {
			throw new InvalidParameterException("Transport is null!");
		}
		// Caricamento
		List<Transport> transportList = getTransportList();
		
	
		// Operazione
		int index = getTransportIndex(transportList, transport);
		if(index != -1) {
			throw new InvalidParameterException("Existing transport!");
		}
		transportList.add(transport);
	
		// Salvataggio
		try {
			fileStoring.save(transportList);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void remove(Transport transport) throws InvalidParameterException {
		if(transport == null) {
			throw new InvalidParameterException("Transport is null!");
		}
		// Caricamento
		List<Transport> transportList = getTransportList();
				
		// Operazione
		int index = getTransportIndex(transportList, transport);
		if(index == -1) {
			throw new InvalidParameterException("Inexisting transport!");
		}
		transportList.remove(index);

		// Salvataggio
		try {
			fileStoring.save(transportList);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void editTransportSeatPrize(Transport transport, double newSeatPrize)  throws InvalidParameterException {
		if(transport == null || newSeatPrize < 0) {
			throw new InvalidParameterException("Transport must be not null and newSeatPrize greather than zero");
		}
		// Caricamento
		List<Transport> transportList = getTransportList();
				
		// Operazione
		int index = getTransportIndex(transportList, transport);
		if(index == -1) {
			throw new InvalidParameterException("Inexisting transport!");
		}
		transportList.get(index).setSeatPrize(newSeatPrize);

		// Salvataggio
		try {
			fileStoring.save(transportList);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public List<Transport> search(Transport transport) {
		List<Transport> result = new ArrayList<Transport>();
		
		for (Transport transportTmp: getTransportList()) {
			boolean match = false;
			
			if(transport.getTransportId() != null && transportTmp.getTransportId().contains(transport.getTransportId())) {
				match = true;
			}
			if(transport.getArrivalDate() != null && transportTmp.getArrivalDate().equals(transport.getArrivalDate())) {
				match = true;
			}
			if(transport.getDepartureDate() != null && transportTmp.getDepartureDate().equals(transport.getDepartureDate())) {
				match = true;
			}
			if(transport.getDestination() != null && transportTmp.getDestination().equals(transport.getDestination())) {
				match = true;
			}
			if(transport.getDestination() != null && transportTmp.getDestination().equals(transport.getDestination())) {
				match = true;
			}
			
			if(match) {
				result.add(transportTmp);
			}
		}
		return result;
	}

	public List<Transport> getTransportList() {
		// Caricamento
		List<Transport> transportList;
		try {
			transportList = (List<Transport>) fileStoring.load();
		} catch(IOException e) {
			transportList = new ArrayList<Transport>();
			transportList.add(new Transport("1", "Napoli", "Torino", new Date(2014, 1, 6), new Date(2014, 1, 6), 15));
			transportList.add(new Transport("2", "Torino", "Napoli", new Date(2014, 7, 30), new Date(2014, 7, 31), 15));
			// Salvataggio
			try {
				fileStoring.save(transportList);
			} catch (IOException ex) {
				e.printStackTrace();
			}
		}
		System.out.println("Lista corse: " + transportList);
		return transportList;
	}
}




